(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:roomtag
     * @description
     * # roomtag 
     */
    angular.module('webApp')
        //调用其他方法
        .run(["$templateCache", "dataService", function($templateCache, dataService) {
            var tplName = "views/directives.roomtag.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }])
        .directive('roomtag', ['dataService', '$compile', 'helper', function(dataService, $compile, helper) {
            return {
                restrict: 'E',
                require: 'ngModel',
                replace: true,
                scope: {
                    ngModel: '=',
                    tagId: "=",
                },

                template: "<span> <input type='text' value='{{ngModel}}' ng-click='choseTag();' readonly/></span>",
                controller: ['$templateCache', '$rootScope', '$scope', '$timeout', function($templateCache, $rootScope, $scope, $timeout) {

                    $scope.choseTag = function() {
                        $scope.pageData().then($scope.showPop);
                    }

                    $scope.showPop = function() {
                        $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '<form class="sui-form" style="margin:0px;"><span>{{"Receiving information"|translate}}</span></form>',
                            body: $templateCache.get("views/directives.roomtag.html"),
                            okBtn: '{{"ok"|translate}}',
                            closeBtn: true,
                            transition: true,
                            width: 'large',
                            height: '200',
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if (!$scope.ngModel || !$scope.tagId) {
                                        helper.myAlert("没有选择标签，无法确认！");
                                        return false;
                                    }
                                });
                            },
                        });
                    }
                    $scope.pageData = function(pageNo) {
                        $scope.pageNo = pageNo ? pageNo : "1";
                        var query = {
                            "content": {
                                page_no: $scope.pageNo,
                                page_size: 100,
                                store_id: $rootScope.store_id,
                            }
                        };

                        return dataService.roomstagGet(query).then(function(result) {
                            if (result.tag_count) {
                                $scope.tag_list = result.tag_list;
                                $scope.tag_count = result.tag_count;
                            }
                        });
                    }

                    $scope.addTag = function() {
                        if (!$scope.tag_name) {
                            helper.myAlert("标签名称不能为空！");
                            return false;
                        }
                        var data = { "tag_name": $scope.tag_name, "store_id": $rootScope.store_id };

                        dataService.roomstagAdd(data).then(function(result) {
                            if (result.Msg == "ok") {
                                $scope.pageData();
                                $scope.tag_name = "";
                            } else {
                                helper.myAlert("error：新建失败，请重试！");
                                return false;
                            }
                        });
                    }

                    //选中
                    $scope.checkLabel = function(tagData, $event) {
                        var className = $($event.currentTarget).attr('class');
                        if (className.indexOf('label-info') != -1) {
                            console.error("ddddddddddddddddddddddd");
                            $scope.ngModel = tagData.tag_name;
                            $scope.tagId = tagData.tag_id;

                            $($event.currentTarget).removeClass('label-info');
                            $($event.currentTarget).addClass('label-danger');

                            $($event.currentTarget).siblings().addClass('label-info');
                            $($event.currentTarget).siblings().removeClass('label-danger');
                        } else {
                            $($event.currentTarget).removeClass('label-danger');
                            $($event.currentTarget).addClass('label-info');
                            $scope.ngModel = "";
                            $scope.tagId = 0;
                        }
                    }
                }],
                link: function(scope, element, attrs, ctrl) {

                }
            }
        }])
})();